package com.seeyon.apps.taxFund.job;

import com.seeyon.apps.taxFund.manager.GKInvoiceManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.quartz.QuartzHolder;
import com.seeyon.ctp.common.quartz.QuartzJob;

import cn.hutool.core.lang.Assert;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 定时给浪潮传表单数据（表单流程结束后给浪潮传输失败的表单数据）
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @CopyRightInformation : XXX(公司名称)
 * @Prject: 
 * @ApplicationName: GKCW_Plugin
 * @Title: FormdataTask.java
 * @Package: com.seeyon.apps.gkcwplugin.quartz
 * @author: wtxx
 * @date: 2023年5月26日 下午4:04:47
 * @version: V1.0
 */
public class ProductSyncTask implements QuartzJob {
	private static final Logger logger= LoggerFactory.getLogger(ProductSyncTask.class);

	private GKInvoiceManager gkInvoiceManager = (GKInvoiceManager)AppContext.getBean("gkInvoiceManager");

	@Override
	public void execute(Map<String, String> map) {
		Date date=new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		logger.info("-------------定时任务，进入-ProductSyncTask-execute,time:"+sf.format(date));
        try {
            gkInvoiceManager.productSync();
        } catch (Exception e) {
            logger.info("-------------定时任务，ProductSyncTask-execute-异常", e);
        }finally {
            logger.info("-------------定时任务，退出-ProductSyncTask-execute");
        }
    }
	public static void registerSyncTask(boolean canExcute) {
        logger.info("=================ProductSyncTask-registerSyncTask注册任务开始====================");
        String jobBeanId = "productSyncTask";
        String jobName = "productSyncTask";
        if (!canExcute) {
            QuartzHolder.deleteQuartzJob(jobName);
        } else {
            // 先删除任务
            QuartzHolder.deleteQuartzJob(jobName);
            Map<String, String> parameters = new HashMap<String, String>();
            try {
                String cron = AppContext.getSystemProperty("shuijin.task.productInfoCron");
                Assert.notBlank(cron, "定时任务【productSyncTask】cron表达式不能为空");
                QuartzHolder.newCronQuartzJob(null, jobName, cron, new Date(), null, jobBeanId, parameters);
            } catch (Exception e) {
                logger.error("ProductSyncTask-registerSyncTask注册任务失败", e);
                logger.info("=================ProductSyncTask-registerSyncTask注册任务失败====================");
            }
        }
        logger.info("=================ProductSyncTask-registerSyncTask注册任务结束====================");
	}
}
